package com.wxq.draw;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.graphics.PointF;

public class PolygonTool {

	/**
	 * 判断商铺list中点击是否在商铺范围内，返回该商铺Map
	 * @param list
	 * @param center
	 * @return
	 */
	public static String getClickedShopInMap(List<Map<String, Object>> list, PointF center){
		//Log.d("wxq", "点击的地图位置：x="+center.x+",y="+center.y);
		if(list != null){
			for (int i = 0; i < list.size(); i++){
				Map<String, Object> shop = list.get(i);
				if (! "Shop".equals(shop.get(DrawView.TEXT_KEY))&&!"Facilities".equals(shop.get(DrawView.TEXT_KEY))) {
				continue;
				}
				if (pointInPolygon(center, parsePoints((String) shop.get(DrawView.PATH_KEY)))){
					//Log.d("wxq", "找到了匹配的区块！");
					return (String) shop.get(DrawView.SPOT_ID_KEY);
				}
//				if (isPolygonContainPoint(center,parsePoints((String) shop.get(DrawView.PATH_KEY)))) {
//					return (String) shop.get(DrawView.SPOT_ID_KEY);
//				}
			}
		}
		return null;
	}
//	/**
//	 * 判断商铺list中点击是否在商铺范围内，返回该商铺Shop (现在不支持此方法)
//	 * @param list
//	 * @param center
//	 * @return
//	 */
//	public static Shop getClickedShop(List<Shop> list, PointF center)
//	{
//		System.err.println("此版本不支持该方法！");
//		for (int i = 0; i < list.size(); i++)
//		{
//			Shop shop = list.get(i);
//			if (shop.getPoints() == null)
//			{
//				shop.setPoints(parsePoints(shop.getPolygon()));
//			}
//			if (pointInPolygon(center, shop.getPoints()))
//			{
//				return shop;
//			}
//		}
//		return null;
//	}
	/**
	 * 判断点是否在多边形内
	 * @param center
	 * @param points
	 * @return
	 */
	public static boolean pointInPolygon(PointF center, List<PointF> points)
	{
		float x0, y0, x1, y1, x2, y2;
		boolean flag = false;
		//当前数据库中宜食家的区域模块是null,此为处理这种特殊情况
		if(points == null)
			return false;
		
		x0 = center.x;
		y0 = center.y;
		
		for (int i = 0; i < points.size() - 1; i++)
		{
			x1 = points.get(i).x;
			y1 = points.get(i).y;
			x2 = points.get(i+1).x;
			y2 = points.get(i+1).y;
			
			if ((x1 < x0) && (x2 < x0))
			{
				//p1,p2在x=x0轴的左侧，无交点
				continue;
			}
			if (((y1 < y0) && (y2 < y0)) || ((y1 > y0) && (y2 > y0)))
			{
				//p1和p2在y=y0轴的上方或下方，无交点
				continue;
			}
			if ((x1 > x0) && (x2 > x0) && (((y1 > y0) && (y2 < y0)) || ((y1 < y0) && (y2 > y0))))
			{
				//p1,p2都在x=x0轴的右侧，在y=y0轴的上下方各一个，必有交点
				flag = !flag;
				continue;
			}
			float x = x1 + (y0 - y1) * (x2 - x1) / (y2 - y1);
			if (x >= x0)
			{
				flag = !flag;
			}
		}

//		if (flag) {
//			Log.d("wxq", "pointArray="+points.toString());
//		}
		return flag; 
	}
	
	
	@SuppressWarnings("unused")
	private static boolean isPolygonContainPoint(PointF pointF,List<PointF> vertexPointFs){  
	    int nCross = 0;  
	    for (int i = 0; i < vertexPointFs.size() - 1; i++) {  
	        PointF p1 = vertexPointFs.get(i);  
	        PointF p2 = vertexPointFs.get(i+1);  
	        if (p1.y == p2.y)  
	            continue;  
	        if (pointF.y < Math.min(p1.y, p2.y))  
	            continue;  
	        if (pointF.y >= Math.max(p1.y, p2.y))  
	            continue;  
	        double x = (double) (pointF.y - p1.y) * (double) (p2.x - p1.x)  
	                / (double) (p2.y - p1.y) + p1.x;  
	        if (x > pointF.x)  
	            nCross++;  
	    }  
	    return (nCross % 2 == 1);  
	} 
	
	/**
	 * 分解点阵字符串
	 * @param polygonStr
	 * @return
	 */
	public static List<PointF> parsePoints(String polygonStr)
	{
		if(polygonStr == null)
			return null;
		//Log.d("wxq", "path="+polygonStr);
		String[] items = polygonStr.split(";");
		
		ArrayList<PointF> pointList = new ArrayList<PointF>();
		
		for (int i = 0; i < items.length; i++)
		{
			String [] point = (items[i]).split(",");
			PointF temp = new PointF(Float.parseFloat(point[0]),
								     Float.parseFloat(point[1])); 
			pointList.add(temp);
		}
		
		return  pointList;
	}
}
